package pswvclass.pswvandroid2.com.need;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.Home;

/**
 * Created by Administrator on 2018/11/2.
 */
public class Goal extends Fragment implements View.OnClickListener {
    private String tag = "Goal";
    private Button need_goal_search;
    private TextView goal_text_search;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_need_goal, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        goal_text_search = (TextView) getView().findViewById(R.id.goal_text_search);
        need_goal_search = (Button) getView().findViewById(R.id.need_goal_search);
        need_goal_search.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.need_goal_search:
                try {
                    if (TextUtils.isEmpty(goal_text_search.getText().toString().trim())) {
                        Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Skyset sk = new Skyset();
                    Bundle bundle = new Bundle();
                    bundle.putString("need", goal_text_search.getText().toString().trim());
                    sk.setArguments(bundle);
                    getFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.realtabcontent, sk).commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.v(tag, e + "");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_BACK) {
                    Button my_page = (Button) getActivity().findViewById(R.id.my_page);
                    Button kword_page = (Button) getActivity().findViewById(R.id.kword_page);
                    Button risk_page = (Button) getActivity().findViewById(R.id.risk_page);
                    Button need_page = (Button) getActivity().findViewById(R.id.need_page);
                    my_page.setVisibility(getView().VISIBLE);
                    kword_page.setVisibility(getView().VISIBLE);
                    risk_page.setVisibility(getView().VISIBLE);
                    need_page.setVisibility(getView().VISIBLE);
                    my_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    kword_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    risk_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    need_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    getFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.realtabcontent, new Home()).commit();
                    return true;
                }
                return false;
            }
        });
    }
}
